namespace MoreLinq.Test;

using System;
using System.Collections;
using System.Collections.Generic;

partial class TestExtensions
{
    public static WatchableEnumerator<T> AsWatchable<T>(this IEnumerator<T> source) => new(source);
}

sealed class WatchableEnumerator<T>(IEnumerator<T> source) :
    IEnumerator<T>
{
    readonly IEnumerator<T> source = source ?? throw new ArgumentNullException(nameof(source));

    public event EventHandler? Disposed;
    public event EventHandler? GetCurrentCalled;
    public event EventHandler<bool>? MoveNextCalled;

    public T Current
    {
        get
        {
                GetCurrentCalled?.Invoke(this, EventArgs.Empty);
                return this.source.Current;
            }
    }

    object? IEnumerator.Current => this.Current;

    public void Reset() => this.source.Reset();

    public bool MoveNext()
    {
            var moved = this.source.MoveNext();
            MoveNextCalled?.Invoke(this, moved);
            return moved;
        }

    public void Dispose()
    {
            this.source.Dispose();
            Disposed?.Invoke(this, EventArgs.Empty);
        }
}
